<template>
	<xn-form-container
		:title="formData.id ? '编辑机构' : '增加机构'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="上级：" name="parentId">
				<a-tree-select
					v-model:value="formData.parentId"
					style="width: 100%"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择上级"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					:field-names="{
						children: 'children',
						label: 'name',
						value: 'id'
					}"
					selectable="false"
					tree-line
				/>
			</a-form-item>
			<a-form-item label="编码：" name="code">
				<a-input v-model:value="formData.code" placeholder="请输入编码" allow-clear />
			</a-form-item>
			<a-form-item label="名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入机构名称" allow-clear />
			</a-form-item>

			<a-form-item label="启用状态：" name="enable">
				<a-radio-group v-model:value="formData.enable" placeholder="请选择启用状态" :options="enableOptions" />
			</a-form-item>
			<a-form-item label="排序：" name="sortCode">
				<a-input-number style="width: 100%" v-model:value="formData.sortCode" :max="100" />
			</a-form-item>

		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>

	</xn-form-container>
</template>

<script setup name="dormForm">
	import { required } from '@/utils/formRules'
	import dormApi from '@/api/biz/dormApi'
	import tool from '@/utils/tool'

	// 定义emit事件
	const emit = defineEmits({ successful: null })
	// 默认是关闭状态
	let visible = $ref(false)
	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({})
	// 定义机构元素
	const treeData = ref([])
	const extJson = ref([])
	const submitLoading = ref(false)
	const enableOptions = ref([])
	// 打开抽屉
	const onOpen = (record, parentId) => {
		visible = true
		extJson.value = ref([])
		formData.value = {
			sortCode: 99
		}
		if (parentId) {
			formData.value.parentId = parentId
		}
		if (record) {
			const param = {
				id: record.id
			}
			dormApi.dormDetail(param).then((data) => {
				formData.value = Object.assign({}, data)
				if (typeof formData.value.extJson !== "undefined") {
					extJson.value = JSON.parse(formData.value.extJson) || []
				}
			})
		}
		// 获取机构树并加入顶级
		dormApi.dormTree().then((res) => {
			treeData.value = [
				{
					id: 0,
					parentId: '-1',
					name: '顶级',
					children: res
				}
			]
		})
		enableOptions.value = tool.dictList('COMMON_SWITCH')
	}
	// 关闭抽屉
	const onClose = () => {
		visible = false
	}
	// 默认要校验的
	const formRules = {
		name: [required('请输入机构名称')],
		sortCode: [required('请选择排序')]
	}
	// 机构分类字典
	const dormCategoryOptions = tool.dictList('ORG_CATEGORY')


	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				//formData.value.extJson = JSON.stringify(extJson.value)
				dormApi
					.dormSubmitForm(formData.value, !formData.value.id)
					.then(() => {
						visible = false
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(e => {console.log(e)})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
